<?php
/**
 * Created by PhpStorm.
 * User: zhengze
 * Date: 2019/7/1
 * Time: 18:29
 */

namespace app\common\validate;

use app\admin\controller\MemberMenu;

use app\api\service\Token;
use app\common\exception\ExceptionEmun;
use app\common\exception\GroupException;
use app\common\exception\MenuException;
use app\common\exception\RuleException;
use Helper\ArrayHelper;
use app\common\model\MemberGroup;
use app\common\traits\InstanceTrait;
use app\common\validate\BaseValidate;
use app\lib\exception\TokenException;

/**
 * Class MemberGroupValidate
 * 验证类的基类
 */
class MemberGroupValidate extends BaseValidate
{
    use InstanceTrait;

    protected $rule = [
        'id|ID' => ['require',],
        'ids|主键数组' => ['require',],
        'status|状态' => ['require',],
        'title|用户组名' => ['require',],
        'rules|规则' => ['require',],
        'menus|菜单' => ['require',],
    ];

    protected $sceneRule = [
        'index' => [
            'only' => [''],
        ],
        'read' => [
            'only' => ['id'],
        ],
        'delete' => [
            'only' => ['id'],
        ],
        'deletes' => [
            'only' => ['ids'],
        ],
        'enables' => [
            'only' => ['ids', 'status'],
        ],
        'save' => [
            'only' => ['title', 'rules', 'menus'],
            'append' => [
                'title' => 'checkNameUnique',
                'rules' => 'checkRuleSExist',
                'menus' => 'checkMenusExist',
            ],
        ],
        'update' => [
            'only' => ['title', 'rules', 'menus'],
            'append' => [
                'title' => 'checkNameUnique',
                'rules' => 'checkRuleSExist',
                'menus' => 'checkMenusExist',
            ],
        ],
    ];

    /**
     * 检测用户组名重复
     * @param $value
     * @param $rule
     * @param array $data
     * @return bool
     * @throws \Exception
     */
    protected function checkNameUnique($value, $rule, $data = [])
    {
        $MemberGroup = MemberGroup::instance();
        $memberGroup = $MemberGroup->getByField('title', $value);
        $pk = 'id';
        if (!empty($memberGroup)) {
            if (!empty($data[$pk])) {
                //修改帐号
                if ($data[$pk] != $memberGroup['group_id']) {
                    return ExceptionEmun::throwException(GroupException::NAME_REPEAT);
                }
            } else {
//                创建帐号
                return ExceptionEmun::throwException(GroupException::NAME_REPEAT);
            }
        }
        return true;
    }

    /**
     * 检测菜单是否存在
     * @param $value
     * @return bool
     * @throws \Exception
     */
    protected function checkMenusExist($value, $rule, $data = [])
    {
        $value = ArrayHelper::ToTree($value);
        $value = array_unique($value);
        $menu_ids = MemberMenu::where('menu_id', 'in', $value)->column('menu_id');
        if (array_intersect($value, $menu_ids) == $value) {
            return true;
        }
        return ExceptionEmun::throwException(MenuException::NO_EXIST);
    }

    /**
     * 检测规则是否存在
     * @param $value
     * @return bool
     * @throws \Exception
     */
    protected function checkRuleSExist($value, $rule, $data = [])
    {
        $value = ArrayHelper::ToTree($value);
        $value = array_unique($value);
        $rule_ids = MemberRule::where('rule_id', 'in', $value)->column('rule_id');
        if (array_intersect($value, $rule_ids) == $value) {
            return true;
        }
        return ExceptionEmun::throwException(RuleException::NO_EXIST);
    }
}
